﻿using UnityEngine;
using System;
using System.Collections;
using UnityEditor;

namespace Hont.HyperWigglePackage.Editor
{
    public partial class HyperWiggleEditorView
    {
        Action mRepaintCB;

        bool mIsHoldCtrl;
        bool mIsHoldAlt;
        bool mIsHoldShift;

        bool mIsMouseMidDown;
        bool mIsMouseRightDown;

        bool mIsMouseDown;
        bool mLastIsMouseDown;
        bool mIsMouseDownOnce;
        bool mIsMouseUp;
        bool mLastIsMouseUp;
        bool mIsMouseUpOnce;

        int mLastButton;
        //float mMouseHoldTimeMark;
        //float mMouseHoldTime;
        Vector2 mMouseDownPosition;
        Vector2 mMousePosition;
        Vector2 mLastMousePosition;
        Vector2 mMouseWheelDelta;

        bool RightButtonClicked
        {
            get
            {
                var rightClickFlag = false;
                rightClickFlag |= mIsMouseUpOnce && mLastButton == 1;
                rightClickFlag |= mIsHoldCtrl && mIsHoldShift && mLastButton == 1;

                return rightClickFlag;
            }
        }


        public void InitGUIEvent(Action repaintCB)
        {
            mRepaintCB = repaintCB;
        }

        public void RefreshEvent()
        {
            mMousePosition = Event.current.mousePosition;
        }

        public void LaterRefreshEvent()
        {
            if ((Event.current.keyCode == KeyCode.LeftControl || Event.current.keyCode == KeyCode.RightControl)
               && Event.current.type == EventType.KeyDown)
            {
                mIsHoldCtrl = true;

                if (mRepaintCB != null)
                    mRepaintCB();
            }

            else if ((Event.current.keyCode == KeyCode.LeftControl || Event.current.keyCode == KeyCode.RightControl)
                && Event.current.type == EventType.KeyUp)
            {
                mIsHoldCtrl = false;

                if (mRepaintCB != null)
                    mRepaintCB();
            }

            else if ((Event.current.keyCode == KeyCode.LeftAlt || Event.current.keyCode == KeyCode.RightAlt)
                && Event.current.type == EventType.KeyDown)
            {
                mIsHoldAlt = true;

                if (mRepaintCB != null)
                    mRepaintCB();
            }

            else if ((Event.current.keyCode == KeyCode.LeftAlt || Event.current.keyCode == KeyCode.RightAlt)
                && Event.current.type == EventType.KeyUp)
            {
                mIsHoldAlt = false;

                if (mRepaintCB != null)
                    mRepaintCB();
            }

            else if ((Event.current.keyCode == KeyCode.LeftShift || Event.current.keyCode == KeyCode.RightShift)
                && Event.current.type == EventType.KeyDown)
            {
                mIsHoldShift = true;

                if (mRepaintCB != null)
                    mRepaintCB();
            }

            else if ((Event.current.keyCode == KeyCode.LeftShift || Event.current.keyCode == KeyCode.RightShift)
                && Event.current.type == EventType.KeyUp)
            {
                mIsHoldShift = false;

                if (mRepaintCB != null)
                    mRepaintCB();
            }

            if (Event.current.type == EventType.MouseDown
                || Event.current.type == EventType.MouseUp)
            {
                mIsMouseDown = Event.current.type == EventType.MouseDown;
                mIsMouseUp = Event.current.type == EventType.MouseUp;
            }

            if (!mLastIsMouseDown && mIsMouseDown)
                mIsMouseDownOnce = true;
            else
                mIsMouseDownOnce = false;

            if (!mLastIsMouseUp && mIsMouseUp)
                mIsMouseUpOnce = true;
            else
                mIsMouseUpOnce = false;

            if (mIsMouseDownOnce)
            {
                //mMouseHoldTimeMark = (float)EditorApplication.timeSinceStartup;
            }

            if (mIsMouseUpOnce)
            {
                //mMouseHoldTime = (float)EditorApplication.timeSinceStartup - mMouseHoldTimeMark;
            }

            if (Event.current.type == EventType.ScrollWheel)
            {
                mMouseWheelDelta = Event.current.delta;
            }
            else
            {
                mMouseWheelDelta = Vector2.zero;
            }

            if (Event.current.button == 1 && mIsMouseDown)
                mIsMouseRightDown = true;

            if (Event.current.button == 1 && mIsMouseUp)
                mIsMouseRightDown = false;

            if (Event.current.button == 2 && mIsMouseDown)
                mIsMouseMidDown = true;

            if (Event.current.button == 2 && mIsMouseUp)
                mIsMouseMidDown = false;

            if (mIsMouseDownOnce)
                mMouseDownPosition = mMousePosition;

            mLastIsMouseDown = mIsMouseDown;
            mLastIsMouseUp = mIsMouseUp;
            mLastButton = Event.current.button;
            mLastMousePosition = mMousePosition;
        }
    }
}
